//
//  feedbackViewController.m
//  HeSaid
//
//  Created by ZC on 15/5/16.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "feedbackViewController.h"
#import "AppDelegate.h"
#import "SetUpViewController.h"
#import "checkBoxCell.h"
#define feedbackURL @"http://app.jiwaipro.com:8888/hesaid/index.php/HeSaid/feedbackoptions"
#define feedback1URL @"http://app.jiwaipro.com:8888/hesaid/index.php/HeSaid/saveFeedBack"

@interface feedbackViewController (){
    AppDelegate *app;
    NSMutableArray *array;
    int checkStr;
    checkBoxCell *cell;
    NSMutableArray *feedstr;
    NSString *feedSTR1;
    NSString *red;
    NSString *img;
    NSString *voi;
    NSString *stm;
    NSString *and;
    NSMutableArray *arrayType;
    UIAlertView *_alert;
}

@end

@implementation feedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    feedstr = [NSMutableArray array];
    arrayType=[NSMutableArray array];
    
    //tableview不可点击
    self.tableView.allowsSelection=NO;
    
    self.tableView.scrollEnabled=NO;
    checkStr = 1;

    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    array=[[NSMutableArray alloc]initWithObjects:@"我要增加摇一摇有红包的功能",@"我要增加可以上传照片的功能",@"我要增加可以上传语音的功能",@"我要增加帖子的显示时间",@"强烈要求上架安卓版",@"这个app做的跟屎一样的，趁早关了吧", nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 提交按钮
- (IBAction)submitButton:(id)sender {
    [self.tableView reloadData];
    arrayType=[NSMutableArray array];
    NSString *poststr =@"";
    [self londingPath:feedbackURL Str:poststr];
}
-(void)londingPath:(NSString *)path Str:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        feedstr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:feedstr waitUntilDone:YES];
    }];
}
-(void)refreshData:(NSMutableArray *)sender{
    for (NSDictionary *dic in feedstr) {
        red = dic[@"options_redenvelope"];
        img = dic[@"options_image"];
        voi = dic[@"options_voice"];
        stm = dic[@"options_showtime"];
        and = dic[@"options_androidapp"];
    }
    int r = [red intValue]+[arrayType[0] intValue];
    int i = [img intValue]+[arrayType[1] intValue];
    int v = [voi intValue]+[arrayType[2] intValue];
    int s = [stm intValue]+[arrayType[3] intValue];
    int a = [and intValue]+[arrayType[4] intValue];
    NSString *poststr =[NSString stringWithFormat:@"options_redenvelope=%d&options_image=%d&options_voice=%d&options_showtime=%d&options_androidapp=%d",r,i,v,s,a];
    [self londingPath1:feedback1URL Str1:poststr];
}
-(void)londingPath1:(NSString *)path Str1:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        feedSTR1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData1:) withObject:feedSTR1 waitUntilDone:YES];
    }];
}
-(void)refreshData1:(NSMutableArray *)sender{
    if ([feedSTR1 isEqualToString:@"1"]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交成功！感谢您对叽歪的支持！！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        _alert = alert;
        [alert show];
        app.feedbackstr = @"1";
        app.defaults = [NSUserDefaults standardUserDefaults];
        [app.defaults setObject:app.feedbackstr forKey:@"feedBack"];
        [app.defaults synchronize];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提交失败！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        SetUpViewController *setUp = [storyboard instantiateViewControllerWithIdentifier:@"setUpVC"];
        [self.navigationController pushViewController:setUp animated:YES];
    }
    }
#pragma mark - 评论tableview返回的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

#pragma mark - 评论每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
    
}

#pragma mark - 每行cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UINib *nib=[UINib nibWithNibName:@"checkBoxCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"checkBox"];
    static NSString *cellIdentifier=@"checkBox";
    cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.index=(long)indexPath.row;
    cell.labelText.text=array[indexPath.row];
    
    [arrayType addObject:cell.str];
    
    return cell;
    
}
@end
